package com.myself.receive;

import com.myself.model.User;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author W.S.
 * @version v4.0.0
 * @description 消费消息的业务类
 * @date Created in 2018/8/21 17:39
 */
@Service
public class ReceiveService {

    private static  Logger logger = LoggerFactory.getLogger(ReceiveService.class);

    /**
     * 测试direct交换机队列
     */
    private static final String TEST_DIREECT_QUEUE = "Q1";

    @RabbitListener(bindings = @QueueBinding(value = @Queue(TEST_DIREECT_QUEUE),key = "test1",exchange = @Exchange("direct.exchange")))
    public void process( Message message, Channel channel) throws Exception{
        try {
            logger.info("消费端消费消息 process str:{},DeliveryTag:{}",new String(message.getBody()),message.getMessageProperties().getDeliveryTag());
            User user = null;
            user.getAge();
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            logger.info("进入异常");
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
        int a = 0;

    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(TEST_DIREECT_QUEUE),key = "test2",exchange = @Exchange("direct.exchange")))
    public void process1(Message message, Channel channel) throws Exception{
        int a = 0;
        try {
            logger.info("消费端消费消息 process-1 str:{},DeliveryTag:{}",new String(message.getBody()),message.getMessageProperties().getDeliveryTag());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
    }


    /**
     * 测试topic交换机
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "Q2"),key = "topic.testsss.#",exchange = @Exchange(value = "topic.exchange",type = "topic")))
    public void topicProcess(String str){
        String test = str;
        logger.info("topic 接受消息 str:{}",str);
    }

    /**
     * 测试topic交换机
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "Q2"),key = "com.*.order",exchange = @Exchange(value = "topic.exchange",type = "topic")))
    public void topicProcess2(String str){
        String test = str;
        logger.info("topicProcess2 接受消息 str:{}",str);
    }


    @RabbitListener(bindings = @QueueBinding(value = @Queue("Q3"),key = "fanout.test",exchange = @Exchange(value = "fanout.exchange",type = "fanout")))
    public void fanoutProcess(String str){
        String test = str;
        logger.info("fanoutProcess 接受广播消息 str:{}",str);
    }

    @RabbitListener(queues = "QUEUE_2")
    public void deldayMsg(String str){
        String test = str;
        logger.info("deldayMsg 接受延时消息 str:{}",str);
    }
}
